package Algorithm.Othes;

import java.util.Deque;
import java.util.LinkedList;

//https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/`
public class Leetcode1614_括号的最大嵌套深度 {
    class Solution {
        public int maxDepth(String s) {
            Deque<Character> stack = new LinkedList<Character>();

            int max = 0;
            for(int i = 0; i < s.length(); i++){
                char c = s.charAt(i);
                if(c == '('){
                    stack.addLast(c);
                }else if(c == ')'){
                    if(stack.isEmpty()){
                        return 0;
                    }
                    max = Math.max(max, stack.size());
                    stack.removeLast();
                }
            }

            if(!stack.isEmpty()){
                return 0;
            }

            return max;
        }
    }
}
